import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:9101',  // 默认灰度环境
        changeOrigin: true,
        rewrite: (p) => {
          const env = localStorage.getItem('env') || 'prd';  // 默认生产环境
          const targetUrl = env === 'gray' ? 'http://localhost:9101' : 'http://localhost:9103';  // 根据环境选择
          return p.replace(/^\/api/, targetUrl);  // 动态代理到目标 URL
        },
      },
      '/gray-api': {
        target: 'http://localhost:9101',  // 灰度环境
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/gray-api/, ''),
      },
      '/prd-api': {
        target: 'http://localhost:9103',  // 生产环境
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/prd-api/, ''),
      },
    },
  },
});
